package com.example.partybuildingsoftware0123456789hwh1127;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.partybuildingsoftware0123456789hwh1127.adapter.FragmentAdapter;
import com.example.partybuildingsoftware0123456789hwh1127.fragment.IndexFragment;
import com.example.partybuildingsoftware0123456789hwh1127.fragment.NewsFragment;
import com.example.partybuildingsoftware0123456789hwh1127.fragment.StudyFragment;
import com.example.partybuildingsoftware0123456789hwh1127.fragment.TripFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

public class PartyActivity extends AppCompatActivity {

    private ViewPager2 vp2PartyFragment;
    private TabLayout tlPartyTab;
    private FragmentAdapter fragmentAdapter;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_party);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
        fragments = new ArrayList<>();
        fragments.add(IndexFragment.newInstance("党建","param2"));
        fragments.add(NewsFragment.newInstance("动态","param2"));
        fragments.add(StudyFragment.newInstance("学习","param2"));
        fragments.add(TripFragment.newInstance("研学","param2"));

        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), getLifecycle());
        fragmentAdapter.setFragments(fragments);
        vp2PartyFragment.setAdapter(fragmentAdapter);

        new TabLayoutMediator(tlPartyTab, vp2PartyFragment, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
               tab.setText(fragments.get(position).getArguments().getString("param1"));
            }
        }).attach();
    }

    private void initView() {
        vp2PartyFragment = (ViewPager2) findViewById(R.id.vp2PartyFragment);
        tlPartyTab = (TabLayout) findViewById(R.id.tlPartyTab);
    }
}